/**
 * 爬虫
 * @example https://segmentfault.com/a/1190000005343112
 * @example https://segmentfault.com/a/1190000006157610
 *
 * @github https://github.com/lxzan/book-spider
 */

var express = require('express');
var superagent = require('superagent');
var cheerio = require('cheerio')

var app = express();
app.get('/', (req, res, next) => {
  // 抓取网页内容
  superagent.get('http://dig.chouti.com/')
      .end((err, sres) => {
        if (err) {
          return next(err);
        }
        // 将获取到的内容赋值给$
        var $ = cheerio.load(sres.text);
        var items = [];
        $('#content-list .part2').each(function (idx, element) {
          var $element = $(element);
          items.push({
            title: $element.attr('share-title'),
            href: $element.attr('href'),
            img: $element.attr('share-pic')
          });
        });

        res.send(items);
      });
});

app.listen(3000, () => console.log('It runing!!!!!!'));